//
//  ViewController.m
//  UI14_homework(礼物说发现->攻略页面)
//
//  Created by Hanlingyu on 16/8/2.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "ClassesCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "FirstHeaderCollectionReusableView.h"
#import "HeaderWithButtonCollectionReusableView.h"

#define kCollectionViewReuse @"reuse"
#define kCollectionViewHeaderViewReuse @"headerViewReuse"
#define kCollectionViewHeaderViewWithButtonReuse @"buttonViewReuse"
#define kCollectionViewHeaderViewFirstHeaderViewResue @"firstHeaderViewReuse"
@interface ViewController ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), 30)];
    searchBar.barStyle = 0;
    searchBar.barTintColor = [UIColor clearColor];
    searchBar.placeholder = @"快选一份七夕礼物, 送给心爱的Ta吧";
    [self.view addSubview:searchBar];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    CGFloat itemWidth = (CGRectGetWidth(self.view.bounds) - 30) / 2;
    flowLayout.itemSize = CGSizeMake(itemWidth, 70);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 10;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 94, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 94) collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[ClassesCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    
    [collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewReuse];
    [collectionView registerClass:[HeaderWithButtonCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewWithButtonReuse];
    [collectionView registerClass:[FirstHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewFirstHeaderViewResue];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 4;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    }
    
    return 6;
    
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ClassesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: kCollectionViewReuse forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    return cell;
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(CGRectGetWidth(self.view.bounds), 300);
    }
    
    return CGSizeMake(CGRectGetWidth(self.view.bounds), 40);
    
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
//    HeaderCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewReuse forIndexPath:indexPath];
//    reusableView.backgroundColor = [UIColor blueColor];
    
   
//    if (indexPath.section == 0) {
//        FirstHeaderCollectionReusableView *firstReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewFirstHeaderViewResue forIndexPath:indexPath];
//        firstReusableView.titleLabel.text = @"栏目";
//        return firstReusableView;
//    } else if (indexPath.section == 1) {
//        HeaderWithButtonCollectionReusableView *reusableViewWithButton = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewWithButtonReuse forIndexPath:indexPath];
//        reusableViewWithButton.titleLabel.text = @"风格";
//        return reusableViewWithButton;
//
//    } else {
//        
//        reusableView.titleLabel.text = @"对象";
//
//    }
//    
//    return reusableView;
    
    switch (indexPath.section) {
        case 0:
        {
            FirstHeaderCollectionReusableView *firstReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewFirstHeaderViewResue forIndexPath:indexPath];
            return firstReusableView;
        }
            break;
        case 1:
        {
            HeaderCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewReuse forIndexPath:indexPath];            reusableView.titleLabel.text = @"品类";
            return reusableView;
        }
            break;
        case 2:
        {
            HeaderWithButtonCollectionReusableView *reusableViewWithButton = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewWithButtonReuse forIndexPath:indexPath];
            reusableViewWithButton.titleLabel.text = @"风格";
            return reusableViewWithButton;
        }
            break;
        case 3:
        {
            HeaderWithButtonCollectionReusableView *reusableViewWithButton = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderViewWithButtonReuse forIndexPath:indexPath];
            reusableViewWithButton.titleLabel.text = @"对象";
            return reusableViewWithButton;
        }
            break;
    }
    
    
    return nil;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
